package com.yft.data.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yft.data.Teacher;
import com.yft.data.dao.PublicDao;
import com.yft.data.util.MapToBean;
@Service
public class PersonService {
	
	private static final Logger logger = LoggerFactory.getLogger(PersonService.class);
	@Autowired
	private PublicDao publicDao;
	
	public List<Teacher> getTeacherList(int pageno, int pagesize) {
		List<Teacher> list = new ArrayList<Teacher>();
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT t_id, t_name, idcard, t_card,");
		sb.append("	age,  gender,  brithday,  workdate,  tel,");
		sb.append("	fax,  moblie,  education,  gdate,  edate,");
		sb.append("	position,  pic ");
		sb.append("FROM TEACHER ");
		sb.append("LIMIT ");
		sb.append((pageno - 1) * pagesize);
		sb.append(", " + pagesize);
		String sql = sb.toString();
		logger.info(sql);
		List<Map<String,Object>> lists = publicDao.getObjectList(sql);
		for(Map<String,Object> map:lists){
			list.add((Teacher) MapToBean.map2Bean(map, Teacher.class));
		}
		return list;
	}

	public Teacher getTeacherByID(int id) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT t_id, t_name, idcard, t_card,");
		sb.append("	age,  gender,  brithday,  workdate,  tel,");
		sb.append("	fax,  moblie,  education,  gdate,  edate,");
		sb.append("	position,  pic ");
		sb.append("FROM TEACHER ");
		sb.append("WHERE t_id = " + id);
		String sql = sb.toString();
		Map<String,Object> map = publicDao.getObjectMap(sql);
		logger.info(sql);
		Teacher teacher = null;
		if(null!=map){
			teacher = (Teacher) MapToBean.map2Bean(map, Teacher.class);
			sb.setLength(0);
			sb.append("SELECT E_ID, COMPANY, STARTDATE, ENDDATE, POSITION, REPERSON, TEL, T_ID ");
			sb.append("FROM EXPERIENCE WHERE T_ID = " + id);
			sql = sb.toString();
			List<Map<String,Object>> lists = publicDao.getObjectList(sql);
			teacher.setExperience(lists);
		}
		return teacher;
	}

	public List<Map<String, Object>> getExperienceByTid(long t_id) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT E_ID, COMPANY, STARTDATE, ENDDATE, POSITION, REPERSON, TEL, T_ID ");
		sb.append("FROM EXPERIENCE WHERE T_ID = " + t_id);
		String sql = sb.toString();
		List<Map<String,Object>> lists = publicDao.getObjectList(sql);
		return lists;
		
	}

}
